package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.StudentCertificateDto;
import com.jyy.prefabricated.entity.Certificate;
import com.jyy.prefabricated.entity.StudentCertificate;
import com.jyy.prefabricated.enums.CertificateRuleEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.CertificateMapper;
import com.jyy.prefabricated.mapper.StudentCertificateMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class StudentCertificateService {

    @Autowired
    private StudentCertificateMapper studentCertificateMapper;

    @Autowired
    private CertificateMapper certificateMapper;

    /**
     * 新增
     * @param studentCertificate
     * @return
     */
    public Integer insert(StudentCertificate studentCertificate){
        studentCertificateMapper.insert(studentCertificate);
        return studentCertificate.getId();
    }

    /**
     * 更新
     * @param studentCertificate
     * @return
     */
    public int update(StudentCertificate studentCertificate){
        if(studentCertificate.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return studentCertificateMapper.updateByPrimaryKeySelective(studentCertificate);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return studentCertificateMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public StudentCertificate selectById(Integer id){
        return studentCertificateMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据id列表查询
     * @param ids id列表
     * @return
     */
    public List<StudentCertificate> selectByIds(List<Integer> ids){
        return studentCertificateMapper.selectByIds(ids);
    }

    /**
     * 查询当前证书分配的最后一个学生的id
     * @param id
     * @return
     */
    public StudentCertificate selectLastByCertificateId(Integer id){
        return studentCertificateMapper.selectLastByCertificateId(id);
    }

    /**
     * 批量插入
     * @param dto
     * @return
     */
    public List<StudentCertificate> batchInsert(StudentCertificateDto dto){
        List<StudentCertificate> list = new ArrayList<>();
        List<Integer> ids = new ArrayList<>();
        if(dto.getStudentIds().size() > 0) {
            //查询当前证书规则
            Certificate certificate = certificateMapper.selectByPrimaryKey(dto.getCertificateId());
            String nowCode = certificate.getCode();
            if (StringUtils.isBlank(certificate.getCode())) {
                StudentCertificate studentCertificate = selectLastByCertificateId(certificate.getId());
                if(studentCertificate != null){
                    nowCode = studentCertificate.getCertificateCode();
                }
            }
            for (int i = 0; i < dto.getStudentIds().size(); i++) {
                StudentCertificate studentCertificate = new StudentCertificate();
                studentCertificate.setCertificateId(dto.getCertificateId());
                studentCertificate.setStudentId(dto.getStudentIds().get(i));
                if (StringUtils.isNotBlank(nowCode)) {
                    if (certificate.getCodeRule().equals(CertificateRuleEnum.ASC.getValue())) {               //升序
                        String code = String.valueOf(Long.valueOf(nowCode) + i);
                        studentCertificate.setCertificateCode(code);
                    } else {
                        //降序
                        String code = String.valueOf(Long.valueOf(nowCode) + i);
                        studentCertificate.setCertificateCode(code);
                    }
                }
                studentCertificateMapper.insert(studentCertificate);
                ids.add(studentCertificate.getId());
            }
            list = selectByIds(ids);
        }
        return list;
    }

    /**
     * 批量更新
     * @param studentCertificates
     * @return
     */
    public int batchUpdate(List<StudentCertificate> studentCertificates){
        return studentCertificateMapper.batchUpdate(studentCertificates);
    }

    /**
     * 根据参数查询
     * @param studentName 学员姓名
     * @param idCard 身份证
     * @param certificateName 证书名称
     * @param certificateCode 证书编号
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return
     */
    public List<StudentCertificate> selectByParams(String studentName, String idCard, String certificateName, String certificateCode, String startDate, String endDate){
        return studentCertificateMapper.selectByParams(studentName, idCard, certificateName, certificateCode, startDate, endDate);
    }

    /**
     * 根据学员id查询
     * @param studentId 学员id
     * @return
     */
    public List<StudentCertificate> selectByStudentId(Integer studentId){
        return studentCertificateMapper.selectByStudentId(studentId);
    }

    public List<StudentCertificate> selectCertificate(StudentCertificate studentCertificate){
        return studentCertificateMapper.selectCertificate(studentCertificate);
    }

}
